<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Permission
 *
 * @author Ghazy
 */
class UMS_Admin_Permission {

    //constants
    const ADD_SUCCESS = 0;
    const NAME_EXIST = 1;
    const EDIT_SUCCESS = 2;
    const ADD_FAILED = 3; 


     //functions
     public function addNewPermission( $name, $roles, $resources)
     {
         $obj_perm = new LkpPermission();
         $obj_rel = new RelRolePermission();
         $chk_name = LkpPermission::checkNameExist($name);

       //************check name is unique***********
       if( $chk_name )
       {
          return self::NAME_EXIST;
       }
       else
       {
               $obj_perm->addPermission($name);
               $perm = LkpPermission::getPermissionbyName($name);
               foreach($roles as $role)
               {
                  $r =new RelRolePermission();
                  $r->role_id = $role;
                  $r->permission_id = $perm['id'];
                  $r->resources = $resources;
                  $r->save();
               }
        
               return self::ADD_SUCCESS;
       }
     }


     public function deletePermission($id)
     {
         RelRolePermission::deleteRelationbyPermission($id);
         LkpPermission::deleteperm($id);
     }


     public function changePermissionName($id, $name)
     {
         LkpPermission::changeName($id, $name);
     }
}
?>
